var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		totalprice: '0.00',
		ischecked: true,
		ids:[],
		selectedcount:0,
		isload:false
	},
	//onLoad: function () {
	//	var that = this;
	//	that.getprolist();
	//},
	onShow: function () {
		this.setData({isload:false})
		this.getprolist();
	},
	onPullDownRefresh: function () {
		this.onShow();
		wx.stopPullDownRefresh()
	},
	getprolist: function () {
		var that = this;
		$.get('shop/cart', {}, function (res) {
			if(that.data.isload == false){
				var cartlist = res.cartlist;
				var ids = [];
				for(var j=0;j<cartlist.length;j++){
					//cartlist[j].ischecked = true;
					ids.push(cartlist[j].id)
				}
				res.cartlist = cartlist;
				res.ids = ids;
			}
			res.isload = true;
			that.setData(res);
			var data = res.cartlist
			that.calculate();
		});
	},
	calculate:function(){
		var that = this;
		var ids = that.data.ids;
		var cartlist = that.data.cartlist
		var totalprice = 0.00;
		var selectedcount = 0
		for (var i = 0; i < ids.length; i++) {
			var id = ids[i]
			for(var j=0;j<cartlist.length;j++){
				if(cartlist[j].id == id){
					var thiscart = cartlist[j]
					totalprice += thiscart.guige.sell_price * thiscart.num;
					selectedcount += thiscart.num;
				}
			}
		}
		that.setData({
			totalprice: totalprice.toFixed(2),
			selectedcount:selectedcount
		});
	},
	checkpro: function (e) {
		var that = this;
		var ids = e.detail.value
		that.setData({ids:ids})
		that.calculate();
	},
	cartdelete: function (e) {
		var that = this;
		var id = e.currentTarget.dataset.cartid
		$.confirm('确定要从购物车移除吗?', function () {
			$.post('shop/cartdelete', {id:id}, function (data) {
				$.success('操作成功');
				setTimeout(function () {
					that.setData({isload:false})
					that.getprolist();
				}, 1000)
			})
		});
	},
	toOrder: function () {
		var that = this;
		var ids = that.data.ids;
		if (ids == undefined || ids.length == 0) {
			$.error('请先选择产品');
			return;
		}
		var cartlist = that.data.cartlist
		var prodata = [];
		for (var i = 0; i < ids.length; i++) {
			var id = ids[i]
			for(var j=0;j<cartlist.length;j++){
				if(cartlist[j].id == id){
					prodata.push(cartlist[j].proid + ',' + cartlist[j].ggid + ',' + cartlist[j].num);
				}
			}
		}
		$.goto('buy?prodata=' + prodata.join('-'));
	},
	//加
	gwcplus: function (e) {
		var maxnum = parseInt(e.currentTarget.dataset.max)
		var cartid = e.currentTarget.dataset.cartid
		var num = parseInt(e.currentTarget.dataset.num)
		if (num >= maxnum) {
			$.error('库存不足');
			return;
		}
		var that = this
		$.post('shop/cartChangenum', {id: cartid,num:num+1}, function (data) {
			if (data.status == 1) {
				that.getprolist();
			} else {
				$.error(data.msg);
			}
		});
	},
	//减
	gwcminus: function (e) {
		var maxnum = parseInt(e.currentTarget.dataset.max)
		var cartid = e.currentTarget.dataset.cartid
		var num = parseInt(e.currentTarget.dataset.num)
		if (num == 1) return;
		var that = this
		$.post('shop/cartChangenum', {id: cartid,num:num-1}, function (data) {
			if (data.status == 1) {
				that.getprolist();
			} else {
				$.error(data.msg);
			}
		});
	},
	//输入
	gwcinput: function (e) {
		var maxnum = parseInt(e.currentTarget.dataset.max)
		var cartid = e.currentTarget.dataset.cartid
		var num = e.currentTarget.dataset.num
		var newnum = parseInt(e.detail.value);
		console.log(num + '--' + newnum);
		if (num == newnum) return;
		if (newnum > maxnum) {
			$.error('库存不足');
			return;
		}
		var that = this
		$.post('shop/cartChangenum',{id:cartid,num: newnum}, function (data) {
			if (data.status == 1) {
				that.getprolist();
			} else {
				$.error(data.msg);
			}
		});
	},
}))